package com.example.finaltest.login;

import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.content.Intent;
import android.widget.TextView;
import android.widget.Toast;
import com.example.finaltest.utils.SaveUserUtils;

import com.example.finaltest.R;
import com.example.finaltest.utils.BaseFragment;
import com.example.finaltest.utils.URLUtils;
import com.google.gson.Gson;

import org.xutils.x;

import java.util.HashMap;
import java.util.Map;




public class LoginFragment extends BaseFragment {
    EditText logintelEt, loginpwdEt;
    Button loginensureBtn;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_login, container, false);
        initView(view);
        return view;
    }

    private void initView(View view) {
        logintelEt = view.findViewById(R.id.frag_login_et_te1);
        loginpwdEt = view.findViewById(R.id.frag_login_et_pwd);
        loginensureBtn = view.findViewById(R.id.frag_login_btn);
        loginensureBtn.setOnClickListener(listener);
    }

    View.OnClickListener listener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //获取输入内容
            String tel = logintelEt.getText().toString();
            String pwd = loginpwdEt.getText().toString();
            boolean flag = judgeInput(tel, pwd);
            if (flag) { //可以执行网络请求
                Map<String, String> map = new HashMap<>();
                map.put("phone", tel);
                map.put("password", pwd);
                postNetwork(URLUtils.login_url, map);
            }
        }
    };

    @Override
    public void onSuccess(String result) {
        super.onSuccess(result);
        // 解析数据
        UserBean bean = new Gson().fromJson(result, UserBean.class);
        int code = bean.getCode();
        switch (code) {
            case 200:   //登录成功
                Toast.makeText(getContext(), "登录成功！", Toast.LENGTH_SHORT).show();
                //保存登录信息
                UserBean.DataBean data = bean.getData(); //用户信息
                SaveUserUtils.saveUserToFile(getContext(), data);
                //跳转到个人中心页面
                Intent intent = new Intent(getContext(), Person_CenterActivity.class);
                startActivity(intent);
                //跳转到个人界面
                break;

            case 201:
                Toast.makeText(getContext(), "手机号或密码错误！", Toast.LENGTH_SHORT).show();
                break;

            default:   //其他，登录失败
                Toast.makeText(getContext(), "请输入正确格式", Toast.LENGTH_SHORT).show();
                break;
        }

    }

    private boolean judgeInput(String tel, String pwd) {
        //   判断输入内容为空
        if (TextUtils.isEmpty(tel) || TextUtils.isEmpty(pwd)) {
            Toast.makeText(getContext(), "输入内容不能为空！", Toast.LENGTH_SHORT).show();
            return false;
        }
        //  判断输入的电话号码是否符合要求
        String regex = "^1(?:3\\d|4[4-9]|5[0-35-9]|6[67]|7[013-8]|8\\d|9\\d)\\d{8}$";
        boolean matches = tel.matches(regex);
        if (!matches) {
            Toast.makeText(getContext(), "输入手机号码不合法！", Toast.LENGTH_SHORT).show();
            return false;
        }
        return true;
    }
}
